package strategy;

import java.util.HashMap;
import java.util.Map;
//Java 用策略模式重构一 if else，并用工厂模式管理，这些策略怎么写？
//在 Java 中使用策略模式重构if-else语句并用工厂模式管理策略，通常包括以下几个步骤：定义策略接口、实现具体策略类、创建策略工厂类。
// 下面通过一个简单的示例来展示如何实现，示例场景为根据不同的促销类型计算商品的最终价格，具体代码如下：

// 策略接口：定义计算价格的方法
interface PromotionStrategy {
    double calculate(double originalPrice);
    void setNext(PromotionStrategy next);
    public double handle(double originalPrice);
}
// 具体策略类：无促销策略
class NoPromotion implements PromotionStrategy {
    private PromotionStrategy next;
    @Override
    public double calculate(double originalPrice) {
        return originalPrice;
    }
    @Override
    public void setNext(PromotionStrategy next) {
        this.next = next;
    }
    @Override
    public double handle(double originalPrice) {
        if (next != null) {
            return next.calculate(originalPrice);
        }
        return calculate(originalPrice);
    }
}

// 具体策略类：打折促销策略
class DiscountPromotion implements PromotionStrategy {
    private double discount;
    private PromotionStrategy next;

    public DiscountPromotion(double discount) {
        this.discount = discount;
    }

    @Override
    public double calculate(double originalPrice) {
        return originalPrice * discount;
    }

    @Override
    public void setNext(PromotionStrategy next) {
        this.next = next;
    }
    @Override
    public double handle(double originalPrice) {
        // 这里可以添加判断是否适用当前策略的逻辑
        // 示例直接计算
        return calculate(originalPrice);
    }
}

// 具体策略类：满减促销策略
class FullReducePromotion implements PromotionStrategy {
    private double fullAmount;
    private double reduceAmount;
    private PromotionStrategy next;

    public FullReducePromotion(double fullAmount, double reduceAmount) {
        this.fullAmount = fullAmount;
        this.reduceAmount = reduceAmount;
    }

    @Override
    public double calculate(double originalPrice) {
        if (originalPrice >= fullAmount) {
            return originalPrice - reduceAmount;
        }
        return originalPrice;
    }

    @Override
    public void setNext(PromotionStrategy next) {
        this.next = next;
    }
    @Override
    public double handle(double originalPrice) {
        // 这里可以添加判断是否适用当前策略的逻辑
        // 示例直接计算
        return calculate(originalPrice);
    }
}
// 责任链管理者类
class PromotionChainManager {
    private PromotionStrategy head;

    public void setHead(PromotionStrategy head) {
        this.head = head;
    }

    public double calculatePrice(double originalPrice) {
        if (head != null) {
            return head.handle(originalPrice);
        }
        return originalPrice;
    }
}
// 策略工厂类：根据促销类型获取对应的策略
//class PromotionStrategyFactory {
//    public static PromotionStrategy getPromotionStrategy(String promotionType) {
//        switch (promotionType) {
//            case "NO_PROMOTION":
//                return new NoPromotion();
//            case "DISCOUNT":
//                return new DiscountPromotion(0.8); // 示例打8折
//            case "FULL_REDUCE":
//                return new FullReducePromotion(200, 50); // 示例满200减50
//            default:
//                throw new IllegalArgumentException("不支持的促销类型: " + promotionType);
//        }
//    }
//}
// 策略工厂类：使用Map存储所有策略
class PromotionStrategyFactory {
    private static final Map<String, PromotionStrategy> STRATEGY_MAP = new HashMap<>();

    static {
        STRATEGY_MAP.put("NO_PROMOTION", new NoPromotion());
        STRATEGY_MAP.put("DISCOUNT", new DiscountPromotion(0.8)); // 示例打8折
        STRATEGY_MAP.put("FULL_REDUCE", new FullReducePromotion(200, 50)); // 示例满200减50
    }

    public static PromotionStrategy getPromotionStrategy(String promotionType) {
        PromotionStrategy strategy = STRATEGY_MAP.get(promotionType);
        if (strategy == null) {
            throw new IllegalArgumentException("不支持的促销类型: " + promotionType);
        }
        return strategy;
    }
}


// 测试类
class Main {
    public static void main(String[] args) {
        double originalPrice = 300;
        // 无促销
        PromotionStrategy noPromotion = PromotionStrategyFactory.getPromotionStrategy("NO_PROMOTION");
        double price1 = noPromotion.calculate(originalPrice);
        System.out.println("无促销时价格: " + price1);

        // 打折促销
        PromotionStrategy discountPromotion = PromotionStrategyFactory.getPromotionStrategy("DISCOUNT");
        double price2 = discountPromotion.calculate(originalPrice);
        System.out.println("打折促销时价格: " + price2);

        // 满减促销
        PromotionStrategy fullReducePromotion = PromotionStrategyFactory.getPromotionStrategy("FULL_REDUCE");
        double price3 = fullReducePromotion.calculate(originalPrice);
        System.out.println("满减促销时价格: " + price3);
    }
}

//我们可以在上述促销策略的基础上，使用责任链模式来重构代码，让不同的促销策略按照一定的顺序依次处理商品价格计算，
// 直到有一个策略能够处理该情况为止。以下是使用责任链模式实现的代码示例：
// 测试类
class Main2 {
    public static void main(String[] args) {
        double originalPrice = 300;

        // 创建具体策略实例
        NoPromotion noPromotion = new NoPromotion();
        DiscountPromotion discountPromotion = new DiscountPromotion(0.8);
        FullReducePromotion fullReducePromotion = new FullReducePromotion(200, 50);

        // 设置责任链顺序
        noPromotion.setNext(discountPromotion);
        discountPromotion.setNext(fullReducePromotion);

        // 创建责任链管理者并设置头部
        PromotionChainManager manager = new PromotionChainManager();
        manager.setHead(noPromotion);

        // 计算价格
        double finalPrice = manager.calculatePrice(originalPrice);
        System.out.println("最终价格: " + finalPrice);
    }
}